package com.maxatime.web.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.maxatime.exception.PhoenixException;
import com.maxatime.pojo.*;
import com.maxatime.service.DormitoryService;
import com.maxatime.utils.MyContent;
import com.maxatime.utils.SerialNumberUtil;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.*;

/**
 * @author Mr.Zhao
 * @title: DormitoryController
 * @projectName phoenix
 * @description: TODO
 * @date 2021/8/12 19:42
 */
@Controller
public class DormitoryController {
    Logger log = LogManager.getLogger();
    @Autowired
    private DormitoryService dormitoryService;



         @RequestMapping("/dormitoryFind")
         @ResponseBody
         public Map<String, Object> dormitoryFind(@RequestParam(required = false, defaultValue = "1") int page,
                                              @RequestParam(required = false, defaultValue = "7") int limit,
                                                  DormitoryVo vo


         ) {

             // 使用Pagehelper传入当前页数和页面显示条数会自动为我们的select语句加上limit查询
             // 从他的下一条sql开始分页
             PageHelper.startPage(page, limit);
             List<Dormitory> dor = dormitoryService.dormitoryFind(vo);// 这是我们的sqlf





             for (int i = 0; i < dor.size(); i++) {
                 Integer  ww=    dormitoryService.selectnownum(dor.get(i).getDormitoryId());

                 dor.get(i).setNowNumber(ww);

             }









             // 使用pageInfo包装查询
             PageInfo<Dormitory> rolePageInfo = new PageInfo<>(dor);//
             Map<String, Object> map = new HashMap<String, Object>();
             map.put("code", 0);
             map.put("msg", "");
             map.put("count", rolePageInfo.getTotal());
             map.put("data", rolePageInfo.getList());
             log.info("map------------------------------------" + map);
             return map;

         }

         @RequestMapping("/dormitory")
          public String dormitory(){
             return "/html/dormitory";
         }



         @RequestMapping("/dormitoryDelete")
         @ResponseBody
         public Integer dormitoryDelete(@RequestBody String data) {
             JSONObject jsonObject = JSONObject.parseObject(data);
             String pk = jsonObject.getString("pkId");
             int pkId = Integer.parseInt(pk);


             return dormitoryService.dormitoryDelete(pkId);


         }

         @RequestMapping("/updatedor")
         @ResponseBody
         public Map<String, Object> updatedor(@RequestBody Dormitory dormitory) {
             Map<String, Object> m = new HashMap<String, Object>();


             Calendar now = Calendar.getInstance();//获取时间
             dormitory.setUpdateTime(now.getTime());//当前时间

             try {
                 Integer cc = dormitoryService.updatedor(dormitory);
                 m.put(MyContent.CODE, "0000");
                 m.put(MyContent.MSG, "修改成功");
                 m.put("data", cc);


             } catch (PhoenixException e) {
                 e.printStackTrace();
                 log.error("exception::" + e.getMessage());
                 log.error("exception--code" + e.getCode());
                 log.error("exception--msg" + e.getMsg());
                 m.put(MyContent.CODE, e.getCode());
                 m.put(MyContent.MSG, e.getMsg());
                 m.put("data", 0);
             }

             return m;
         }
         @RequestMapping("/insertdor")
         @ResponseBody
         public Map<String, Object> insertdor(@RequestBody Dormitory dormitory) {
             Map<String, Object> m = new HashMap<String, Object>();

             String dd= SerialNumberUtil.getUUID();

             Calendar now = Calendar.getInstance();//获取时间
             dormitory.setUpdateTime(now.getTime());//当前时间
             dormitory.setCreateTime(now.getTime());
             dormitory.setDeleteFlag("0");
             dormitory.setDormSupervisorId(dd);

             try {
                 Integer cc = dormitoryService.insertdor(dormitory);
                 m.put(MyContent.CODE, "0000");
                 m.put(MyContent.MSG, "添加成功");
                 m.put("data", cc);


             } catch (PhoenixException e) {
                 e.printStackTrace();
                 log.error("exception::" + e.getMessage());
                 log.error("exception--code" + e.getCode());
                 log.error("exception--msg" + e.getMsg());
                 m.put(MyContent.CODE, e.getCode());
                 m.put(MyContent.MSG, e.getMsg());
                 m.put("data", 0);
             }

             return m;
         }
         @RequestMapping("/deleteListDor")
         @ResponseBody
         public Integer deleteListDor(@RequestBody String jsonStr) {

             JSONObject jsonObject = JSONObject.parseObject(jsonStr);
             JSONArray pkId = jsonObject.getJSONArray("pkId");
             Integer[] pkid = new Integer[pkId.size()];
             for (int i = 0; i < pkId.size(); i++) {
                 Object id = pkId.get(i);
                 pkid[i] = Integer.parseInt(id.toString());
                 log.info("id————————————" + Integer.parseInt(id.toString()));
             }
             List<Integer> list = Arrays.asList(pkid);

             return dormitoryService.deleteListDor(list);


         }

    //查与宿舍学生表需要ID 宿舍名字
    @RequestMapping("/selectKeydor/{Id}")
    public String selectKeydor(@PathVariable Integer Id, HttpSession session) {

        Dormitory dormitory = dormitoryService.selectKeydor(Id);
        session.setAttribute("dormitory", dormitory);


        return "html/dormitorystu";


    }


    /*
    *
     * @Author Mr.Zhao
     * @Title addgradestudent
     * @Description 添加学生
     * @Date 20:56 2021/8/19
     * @Param [dorm]
     * @return java.lang.Integer
     **/
    @ResponseBody
    @RequestMapping("/adddorstudent")
    public Integer addgradestudent(@RequestBody StudentDormitoryRelation dorm) {


        StudentDormitoryRelation s=   dormitoryService.selectKeys(dorm.getStudentId());

        Calendar now = Calendar.getInstance();
        dorm.setCreateTime(now.getTime());
        dorm.setUpdateTime(now.getTime());
        dorm.setDeleteFlag("0");

           //添加时修改状态
         Integer aa=   dormitoryService.updatedoruser(dorm.getDormitoryDormitoryId());



          //查询当前宿舍人数
        List<StudentDormitoryRelation> ss=   dormitoryService.select(dorm.getDormitoryDormitoryId());

        if(s==null & ss.size()<dorm.getBedNumber()){
            return  dormitoryService.insert(dorm);

        }else {
            return 2;
        }




    }


    @RequestMapping("/viewstu/{Id}")
    public String viewstu(@PathVariable Integer Id, HttpSession session) {

        session.setAttribute("dd",Id);

        return "html/viewdormitory";


    }
    @RequestMapping("/viewdormitorys")
    @ResponseBody
    public Map<String,Object> viewdormitorys(@RequestParam(required=false,defaultValue="1") int page,
                                            @RequestParam(required=false,defaultValue="7") int limit,
                                            String dormitoryId
    ){
        // 使用Pagehelper传入当前页数和页面显示条数会自动为我们的select语句加上limit查询
        // 从他的下一条sql开始分页
        PageHelper.startPage(page, limit);
        List<StudentMessage> stu  = dormitoryService.viewdormitorys(dormitoryId);// 这是我们的sql
        for(StudentMessage i:stu){
            i.setNativePlace(i.getNativePlace());
        }

        // 使用pageInfo包装查询
        PageInfo<StudentMessage> rolePageInfo = new PageInfo<>(stu);//
        Map<String,Object> map=new HashMap<String,Object>();
        map.put("code",0);
        map.put("msg","");
        map.put("count",rolePageInfo.getTotal());
        map.put("data",rolePageInfo.getList());
        log.info("map------------------------------------"+map);
        return map;
    }

    /*
     *
     * @Author Mr.Zhao
     * @Title dorStuDelete
     * @Description  删除学生
     * @Date 21:25 2021/8/19
     * @Param [data, dormitoryId]
     * @return java.lang.Integer
     **/
    @RequestMapping("/dorStuDelete")
    @ResponseBody
    public Integer dorStuDelete(@RequestBody String data,String dormitoryId) {


        JSONObject jsonObject = JSONObject.parseObject(data);
        String pk = jsonObject.getString("studentId");
        int studentId = Integer.parseInt(pk);

        //查询宿舍学生个数
         Integer ww= dormitoryService.dorNa(dormitoryId);


         if (ww==1){
            //当删除到最后一个修改状态
           Integer ss=  dormitoryService.updatedoUser(dormitoryId);
         }

        return dormitoryService.dorStuDelete(studentId);
    }
    @RequestMapping("/selectsushestu/{dormitoryId}")
    public String viewtu(@PathVariable Integer dormitoryId, HttpSession session) {

        session.setAttribute("pp",dormitoryId);

        return "html/selectsushestu";


    }






}
